import Types from '../types'
import FavoriteDao from '../../expand/dao/FavoriteDao'
import {ProjectModel} from "../../modal/ProjectModel";

/**
 * 获取收藏的项目
 * @param flag
 * @param isLoading
 * @return {{type: string, theme: *}}
 */
export const onLoadFavorite = (flag, isLoading) => {
    return dispatch => {
        if (isLoading) {
            dispatch({
                type: Types.FAVORITE_REFRESH,
                storeName: flag,
                isLoading,
            })
        }

        let favoriteDao = new FavoriteDao(flag)
        favoriteDao.getAllItems()
            .then((result) => {
                let projectModels = []
                for (let i = 0, len = result.length; i < len; i++) {
                    projectModels.push(new ProjectModel(result[i], true))
                }
                dispatch({
                    type: Types.FAVORITE_REFRESH_SUCCESS,
                    storeName: flag,
                    projectModels,
                    isLoading: false,
                })

            }).catch(error => {
            dispatch({
                type: Types.FAVORITE_REFRESH_FAIL,
                storeName: flag,
                isLoading: false,
                error,
            })
        })
    }
}

